package com.yao.controller;

import com.yao.entity.VirtualEntity;
import com.yao.service.VirtualService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class VirtualController {
    @Autowired
    private VirtualService virtualService;
    @Autowired
    private HttpServletRequest request;
    private final static Logger logger = LoggerFactory.getLogger(ServerController.class);

    //跳转到list页面，展示所有数据，分页查询
    @GetMapping(value = "/virtual")
    public String virtual(){
        return "virtual";
    }
    //获取表格数据
    @RequestMapping(value = "/virtual_list")
    @ResponseBody
    public Map<String,Object> virtual_list(Integer page,
                                   Integer limit) {
        List<VirtualEntity> virtuals = virtualService.findAll();
        int pageNum=(page-1)*limit;
        List<VirtualEntity> virtual = virtualService.findPage(pageNum,limit);
        Map<String, Object> tableData = new HashMap<String, Object>();
        //这是layui要求返回的json数据格式
        tableData.put("code", 0);
        tableData.put("msg", "");
        //将全部数据的条数作为count传给前台（一共多少条）
        tableData.put("count", virtuals.size());
        //将分页后的数据返回（每页要显示的数据）
        tableData.put("data", virtual);
        //返回给前端
        return tableData;
    }
    //多条件模糊查询
    @PostMapping("/virtual_select")
    @ResponseBody
    public Map<String, Object> select(
                         @RequestParam(value = "s_virtual_ip",required = false) String virtual_ip,
                         @RequestParam(value = "s_app",required = false) String app,
                         @RequestParam(value = "s_ip",required = false) String ip,
                         @RequestParam(value = "s_admin",required = false) String admin,
                         @RequestParam(value = "s_core",required = false) String core,
                         @RequestParam(value = "s_ram",required = false) String ram,
                         @RequestParam(value = "s_disk",required = false) String disk,
                         @RequestParam(value = "s_systems",required = false) String systems,
                         @RequestParam(value = "s_zabbix",required = false) String zabbix,
                         @RequestParam(defaultValue = "1") Integer page,
                         @RequestParam(defaultValue = "10") Integer limit) {
        Map<String, Object> tableData = new HashMap<String, Object>();
        List<VirtualEntity> virtuals = virtualService.findBySomething(virtual_ip,app,ip,admin,core,ram,disk,systems,zabbix);
        int pageNum = (page - 1) * limit;
        List<VirtualEntity> virtual = virtualService.findByPage(virtual_ip,app,ip,admin,core,ram,disk,systems,zabbix,pageNum,limit);
        tableData.put("code", 0);
        tableData.put("msg", "查询成功");
        //将全部数据的条数作为count传给前台（一共多少条）
        tableData.put("count", virtuals.size());
        //将分页后的数据返回（每页要显示的数据）
        tableData.put("data", virtual);
        //返回给前端
        return tableData;
    }

    // 删除一条信息
    @PostMapping("/virtual_delete")
    @ResponseBody
    public Map<String, Object> delete(@RequestBody int[] ids) {
        Map<String, Object> tableData = new HashMap<String, Object>();
        for (int id : ids) {
            virtualService.delete(id);
        }
        tableData.put("code", 200);
        tableData.put("msg", "删除成功");
        //将全部数据的条数作为count传给前台（一共多少条）
        tableData.put("count", 0);
        //将分页后的数据返回（每页要显示的数据）
        tableData.put("data", "");
        //返回给前端
        return tableData;
    }

    //编辑信息
    @PostMapping("/virtual_edit")
    @ResponseBody
    public Map<String, Object> edit(@RequestParam("edit_id") int id,
                       @RequestParam("edit_virtual_ip") String virtual_ip,
                       @RequestParam("edit_app") String app,
                       @RequestParam("edit_virtual_name") String virtual_name,
                       @RequestParam("edit_ip") String ip,
                       @RequestParam("edit_admin") String admin,
                       @RequestParam("edit_core") String core,
                       @RequestParam("edit_ram") String ram,
                       @RequestParam("edit_disk") String disk,
                       @RequestParam("edit_systems") String systems,
                       @RequestParam("edit_zabbix") String zabbix,
                       @RequestParam("edit_remark") String remark){
        Map<String, Object> tableData = new HashMap<String, Object>();
        virtualService.update(id,virtual_ip,app,virtual_name,ip,admin,core,ram,disk,systems,zabbix,remark);
        tableData.put("code", 200);
        tableData.put("msg", "编辑成功");
        //将全部数据的条数作为count传给前台（一共多少条）
        tableData.put("count", 0);
        //将分页后的数据返回（每页要显示的数据）
        tableData.put("data", "");
        //返回给前端
        return tableData;
    }


    // 添加信息
    @PostMapping("/virtual_add")
    @ResponseBody
    public Map<String, Object> add(@RequestParam("add_virtual_ip") String virtual_ip,
                         @RequestParam("add_app") String app,
                         @RequestParam("add_virtual_name") String virtual_name,
                         @RequestParam("add_ip") String ip,
                         @RequestParam("add_admin") String admin,
                         @RequestParam("add_core") String core,
                         @RequestParam("add_ram") String ram,
                         @RequestParam("add_disk") String disk,
                         @RequestParam("add_systems") String systems,
                         @RequestParam("add_zabbix") String zabbix,
                         @RequestParam("add_remark") String remark){
        Map<String, Object> tableData = new HashMap<String, Object>();
        virtualService.add(virtual_ip,app,virtual_name,ip,admin,core,ram,disk,systems,zabbix,remark);
        tableData.put("code", 200);
        tableData.put("msg", "添加成功！");
        //将全部数据的条数作为count传给前台（一共多少条）
        tableData.put("count", 0);
        //将分页后的数据返回（每页要显示的数据）
        tableData.put("data", "");
        //返回给前端
        return tableData;
    }
}
